﻿//_______________________________________________________________________________________________________________________
// Copy Right (c) 2010, 2011 SapiensIT Consulting GmbH, all rights reserved
//_______________________________________________________________________________________________________________________
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT
// NOT LIMITED TO THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
//_______________________________________________________________________________________________________________________
// PROJECT:     sapiens.at.SharePoint (SharePoint 2010/V4)
//_______________________________________________________________________________________________________________________
// VERSION:     2.0
//_______________________________________________________________________________________________________________________
// DESCRIPTION: 
//_______________________________________________________________________________________________________________________
// LICENSE:     Different licens Types apply to different file types:
//               - Source code files are governed by the MICROSOFT PUBLIC LICENSE (Ms-PL)
//               - Binary files are governed by MSDN CODE GALLERY BINARY LICENSE
//               - Documentation files are governed by CREATIVE COMMONS ATTRIBUTION 3.0 LICENSE
//
//               The licenses can be found at the projects source location.
//               If you do not agree, don't use it!
// 
//_______________________________________________________________________________________________________________________
// Start SharePoint Branding, visit http://www.cliggin.com! Visit us on http://www.sapiens.at! Tell your friends! 
//_______________________________________________________________________________________________________________________
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.UI;
using Microsoft.SharePoint.WebControls;

namespace Sapiens.at.SharePoint.Controls
{
    /// <summary>
    /// Shows the SharePoint status bar.
    /// </summary>
    public class StatusBar : UserControl
    {
        /// <summary>
        /// Gets or sets the title of the message (bold).
        /// </summary>
        public string Title { get; set; }
        /// <summary>
        /// Gets or sets the text of the message.
        /// </summary>
        public string Text { get; set; }
        /// <summary>
        /// Gets or sets, whether the statusbar should render invisible.
        /// </summary>
        public bool HideAtBeginning { get; set; }
        /// <summary>
        /// Gets or sets the color of the statusbar.
        /// </summary>
        public StatusBarColor Color { get; set; }

        /// <summary>
        /// Renders the necessarry javascript on the page.
        /// </summary>
        /// <param name="e"></param>
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            RenderStatusBar(Page, Title, Text, HideAtBeginning, Color);
        }

        /// <summary>
        /// Available colors for the status bar.
        /// </summary>
        public enum StatusBarColor
        {
            /// <summary>
            /// No color is set.
            /// </summary>
            None,
            /// <summary>
            /// Red background color
            /// </summary>
            Red,
            /// <summary>
            /// Blue background color
            /// </summary>
            Blue,
            /// <summary>
            /// Green background color
            /// </summary>
            Green,
            /// <summary>
            /// Yellow bachground color
            /// </summary>
            Yellow
        }

        /// <summary>
        /// Renders the necessarry javascript on a given page.
        /// </summary>
        /// <param name="page"></param>
        /// <param name="Title"></param>
        /// <param name="Text"></param>
        /// <param name="HideAtBeginning"></param>
        /// <param name="Color"></param>
        public static void RenderStatusBar(Page page, string Title, string Text, bool HideAtBeginning, StatusBarColor Color)
        {
            ScriptLink.RegisterScriptAfterUI(page, "SP.js", false, true);
            var script = string.Format(@"
                ExecuteOrDelayUntilScriptLoaded(addStatusBar, 'SP.js');
                function addStatusBar(){{
                    var sid = SP.UI.Status.addStatus('{0}', '{1}', {2});
                    {3}
                }}
            ", Title, Text, (!HideAtBeginning).ToString().ToLower(),
            Color != StatusBarColor.None ? string.Format("SP.UI.Status.setStatusPriColor(sid, '{0}');", Enum.GetName(typeof(StatusBarColor), Color).ToLower()) : "");

            ScriptManager.RegisterClientScriptBlock(page, typeof(StatusBar), string.Format("StatusBar:{0}", Title), script, true);
        }
    }
}
